$(function () {
    //全局定义页数与总条数
    var markId = getUrlString('markid');
    var id = getUrlString('id');
    var page = -1,totalSize = 0,tagInfo = [];
    function getTag() {
        getData('post','/niu/apiv4/niu_tag_info.php',{'markid':markId,},function (data) {
            console.log(data);
            let append = '';
            $(data.tagInfo).each(function () {
                tagInfo.push(this.talk_name);
            });
            detail();
        })
    }
    getTag();
    function detail() {
        var data = {
            'markid':markId,
            'weiboid':id
        };
        getData('post','/niu/apiv3/weibo_getdetail.php',data,function (data) {
            console.log(data);
            $('#userface').attr('src',data.data.userface);
            $('#userName').html(data.data.usernick);
            if(data.data.uservip === '1'){
                $('#uservip').show()
            }else {
                $('#uservip').hide()
            }
            var chenghaoStr = (data.data.userlv).substring(3);

            if (chenghaoStr < 6){
                $('#chenghaoImg').attr('src','../../res/img/common/bronzeIcon.png')
            }else if (5 < chenghaoStr && chenghaoStr < 11){
                $('#chenghaoImg').attr('src','../../res/img/common/silverIcon.png')
            }else if (10 < chenghaoStr && chenghaoStr < 16){
                $('#chenghaoImg').attr('src','../../res/img/common/goldIcon.png')
            }else if (15 < chenghaoStr && chenghaoStr < 21){
                $('#chenghaoImg').attr('src','../../res/img/common/platinaIcon.png')
            }else if (20 < chenghaoStr && chenghaoStr < 26){
                $('#chenghaoImg').attr('src','../../res/img/common/diamondIcon.png')
            }
            $('#usernick,.communityUserPic').click(function () {
                var id = data.data.userid;
                window.location.href = 'dancool://member='+id;
            });
            $('#outtime').html(subTime(data.data.time));
            // $('#macode').html(data.macode);
            // $('#intro').html(data.data.intro);
            let string = data.data.intro;
            let introArr = string.split('#');
            // console.log(introArr);
            // console.log(tagInfo);
            for (let i = 0;i<introArr.length;i++){
                for (let j = 0;j < tagInfo.length;j++){
                    if (introArr[i] === tagInfo[j]){
                        introArr[i] = '<a>#'+introArr[i]+'#</a>'
                    }
                }
            }
            // console.log(introArr);
            let str = introArr.join('');
            // console.log(str);
            $('#intro').html(str);
            $('#intro a').click(function () {
                let key = $(this).html();
                let keyStr = key.substring(1,key.length-1);
                console.log(keyStr);
                // window.location.href = '../../view/search/pullEggSearch.html?key='+keyStr+'&markid='+markId;
                window.location.href = 'dancool://fullweb='+urlView+'search/pullEggSearch.html?key='+keyStr;
            });
            if(data.data.picurls !== ''){
                var picUrls =  data.data.picurls;
                var picUrlsArr = picUrls.split(';');
                var append = '';
                for (var i = 0;i < picUrlsArr.length;i++){
                    if (picUrlsArr.length === 1){
                        append += '<img style="width: 96%;" src="'+picUrlsArr[i]+'" alt="">' ;
                    }else if (picUrlsArr.length === 2 || picUrlsArr.length === 4){
                        append += '<img style="width: 46.25%" src="'+picUrlsArr[i]+'-toysbox22" alt="">' ;
                    }else{
                        append += '<img src="'+picUrlsArr[i]+'-toysbox22" alt="">' ;
                    }
                }
                $('#infoImg').append(append);
                imgs = $wrap.all('img',$wrap.all('#infoImg')[0]);
                imgs.forEach(function(v,i){
                    let src;
                    if (imgs.length !== 1){
                         src = v.src.substring(0,v.src.length-10);
                    } else {
                         src = v.src
                    }

                    urls.push(src);
                });
                console.log(urls)
                // $('#infoImg img').click(function () {
                //     var previewPic = $(this).attr('src');
                //     window.location.href = 'dancool://web=http://api.odancool.com/weixinapp/view/preview/previewPic.html?previewPic='+previewPic;
                // })
            }
            if(data.data.zancount === '0'){
                $('.nodz').show()
            }else {
                $('#zancount').html(data.data.zancount);
                $('.nodz').hide()
            }
            if (data.data.plcount === '0'){
                $('.nopl').show()
            }else {
                $('.nopl').hide()
            }
            // if(data.data.plcount === '0' && data.data.ismine !== 1){
            //     $('#commentList').hide();
            //     $('.indexLike').css('margin-bottom','3rem');
            // }else if(data.data.plcount === '0' && data.data.ismine === 1){
            //     $('#commentList').hide();
            //     $('.indexLike').css('margin-bottom','0.6rem');
            // }else {
            //     $('#commentList').show()
            // }
            //判断是否是自己的帖子
            if(data.data.ismine === 1){
                $('#delete').show();
            }else {
                $("#delete").hide();
            }
            $('#dzNum').html(data.data.zancount);
            $('#plNum').html(data.data.plcount);
        })
    }

    $('.indexPLTab .tabLi').click(function () {
        $('.indexPLTab .tabLi').removeClass('active');
        $(this).addClass('active');
        if ($(this).index() === 1){
            $('.minirefresh-scroll').hide();
            $('.indexLike').show()
        }else {
            $('.indexLike').hide();
            $('.minirefresh-scroll').show()
        }
    });

    //点赞列表
    function zanCount () {
        var data = {
            'markid':markId,
            'weiboid':id
        };
        getData('post','/niu/apiv3/weibo_zanlistv2.php',data,function (data) {
            console.log(data);
            var append = '';
            $(data.data).each(function () {
                append += '<li class="toUserKu">' +
                    '           <input type="hidden" class="userId" value="'+this.userid+'">'+
                    '           <img src="'+(this.userface).replace(/^http:/,"https:")+'" alt="">' +
                    '<span>'+this.usernick+'</span>'+
                    '</li>'
            });
            $('#likeList').append(append);

            $('#likeList .toUserKu').click(function () {
                var userId = $(this).find('.userId').val();
                window.location.href = 'dancool://member='+userId;
            })
        })
    }
    zanCount();

    var mRefresh = new MiniRefresh({
        container: '#mrefresh', //这里是插件的容器ID
        down: {
            isLock: true,
            isAuto: false,
            callback: function () {
                // 下拉后的回调事件 ，你可以在这里执行你的后台请求数据函数，达到刷新数据的作用
                $('#commentList').empty();
                totalSize = 0;
                page = -1;
                mRefresh.endDownLoading();
            }
        }
        ,
        up: {
            isAuto: true,
            callback: function () {
                // 向上拉后的回调事件
                page += 1;
                pinglunCount(page)
            }
        }
    });
    //评论列表
    function pinglunCount (page) {
        var data = {
            'markid':markId,
            'weiboid':id,
            'page':page
        };
        getData('post','/niu/apiv3/weibo_getcommentlistv2.php',data,function (data) {
            console.log(data);
            var append = '';
            $(data.data).each(function () {
                var chenghaoStr = (this.userlv).substring(3);
                append += '<li>' +
                    '                <div class="communityUser">' +
                    '                    <input type="hidden" class="userId" value="'+this.userid+'">'+
                    '                    <div class="communityUserPic">' +
                    '                        <img src="'+this.userface+'" alt="">' +
                    '' +
                    '                    </div>' +
                    '                    <div class="communityUserName">' +
                    '                        <p>' +
                    '                            <span>'+this.usernick+'</span>' ;
                    if(this.uservip === '1'){
                        append +=  '              <img src="../../res/img/eggTV/monthCard@2x.png" alt="">' ;
                    }
                    if (chenghaoStr < 6 && chenghaoStr > 0){
                        append += '<img src="../../res/img/common/bronzeIcon.png" alt="">' ;
                    }else if (5 < chenghaoStr && chenghaoStr < 11){
                        append += '<img src="../../res/img/common/silverIcon.png" alt="">' ;
                    }else if (10 < chenghaoStr && chenghaoStr < 16){
                        append += '<img src="../../res/img/common/goldIcon.png" alt="">' ;
                    }else if (15 < chenghaoStr && chenghaoStr < 21){
                        append += '<img src="../../res/img/common/platinaIcon.png" alt="">' ;
                    }else if(20 < chenghaoStr && chenghaoStr < 26){
                        append += '<img src="../../res/img/common/diamondIcon.png" alt="">' ;
                    }
                    append +='                </p>' +
                    '                        <p>'+subTime(this.time)+'</p>' +
                    '                    </div>' +
                    '                </div>' +
                    '                <div class="content1">' +
                    '                    <p class="info">'+this.intro+'</p>' +
                    '                </div>' +
                    '            </li>'
            });
            $('#commentList').append(append);
            totalSize = totalSize + 10;
            if (totalSize > data.datacount)
                mRefresh.endUpLoading(true);
            else{
                mRefresh.endUpLoading(false);
            }
            //
            $('#commentList li .communityUser').click(function () {
                var userId = $(this).find('.userId').val();
                window.location.href = 'dancool://member='+userId;
            })
        })
    }
    // pinglunCount();

    //点赞
    $('#likeBtn').click(function () {
        var params = {
            'markid':markId,
            'weiboid':id,
        };
        getData('get', '/niu/apiv3/weibo_dolike.php', params, function (data) {
            console.log(data);
            if(data.resultcode === '0'){
                laymsg(data.msg);
                setTimeout(function () {
                    window.location.reload();
                },500)
            }else {
                laymsg(data.msg)
            }
        })
    });

    //评论
    $('#commentbtn').click(function () {
        $(".guaXD").show().removeClass('bounceOutDown').addClass('bounceInUp');
    });

    var isAjax = true;
    $('#liuYan').click(function () {
        if(isAjax){
            var intro = $('#intro1').val();
            var params = {
                'markid': markId,
                'weiboid': id,
                'type': 1,
                'intro': intro,
                'zhuanid': id,
                'picurls': '',
            };
            getData('get', '/niu/apiv3/weibo_dopost.php', params, function (data) {
                console.log(data);
                if (data.resultcode === '0') {
                    isAjax = false;
                    laymsg(data.msg);
                    setTimeout(function () {
                        window.location.reload();
                    }, 500)
                } else {
                    isAjax = true;
                    laymsg(data.msg)
                }
            })
        }

    });
    $('.qx').click(function () {
        $(".guaXD").removeClass('bounceInUp').addClass('bounceOutDown');
    });

    //删除帖子
    $('.delete span').click(function () {
        var params = {
            'markid':markId,
            'weiboid':id
        };
        getData('get', '/niu/apiv3/weibo_delpost.php', params, function (data) {
            console.log(data);
            if(data.resultcode === '0'){
                laymsg(data.msg);
                setTimeout(function () {
                    nativeCommand("dancool://activity=reflashfin");
                },500)
            }else {
                laymsg(data.msg)
            }
        })
    })
});
